package batframework.mvc.session;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import batframework.mvc.Session;


public class DefaultSession implements Session
{
	private HttpSession session;

	public void addAttr(String name, Object value)
	{
		session.setAttribute(name, value);
	}

	@SuppressWarnings("unchecked")
	public <T> T getAttr(String name)
	{
		return (T) session.getAttribute(name);
	}

	public void removeAttr(String name)
	{
		session.removeAttribute(name);
	}
	
	public DefaultSession(HttpSession session)
	{
		this.session = session;
	}

	public String getId()
	{
		return session.getId();
	}

	public Map<String, Object> getAttrs()
	{
		Map<String, Object> attrs = new HashMap<String, Object>();
		Enumeration<String> names = session.getAttributeNames();
		while(names.hasMoreElements())
		{
			String next = names.nextElement();
			attrs.put(next, getAttr(next));
		}
		return attrs;
	}
}
